package cn.maple.general.asynchronous;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class MessageProducer {


    public static void main(String[] args) throws Exception {
        //异步消息
        asynchronousMessage();

    }

    /**
     *asynchronousMessage
     * 异步消息生产
     * @throws Exception
     */
    public static void asynchronousMessage() throws Exception {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("Asynchronous-message-Producer"/*生产者组名*/);
        // 设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");
        // 启动Producer实例
        producer.start();

        // 创建消息，并指定Topic，Tag和消息体
        Message msg = new Message("AsynchronousMessage10086" /* Topic (组名)*/,
                "AsynchronousTest1" /* Tag */,
                ("RocketMQ同步消息发送1========").getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        System.out.println("=====异步消息信息1准备========");
        System.out.println(msg);

        // 发送消息到一个Broker
        producer.send(msg, new SendCallback() {
            //异步消息没有返回值sendResult
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("=====异步消息信息1发送成功=====");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("=====异步消息信息1失败=====");
            }
        });

        System.out.println("=====================异步消息发送===========================");


        // 如果不再发送消息，关闭Producer实例。
       // producer.shutdown();

    }


}

